using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp-产品报废单详细表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpScrapProductService : IApplicationService 
{
    private readonly SugarRepository<ErpScrapProduct> _thisRepository;
    public ErpScrapProductService(SugarRepository<ErpScrapProduct> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ErpScrapProductDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Id!=0,m=>m.ScrapId==param.Id)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        var result=query.Adapt<PageResult<ErpScrapProductDto>>();
        if (result.Items.Count>0)
        {
            var resultProduct = new List<ErpProductDto>();
            var productRepository = _thisRepository.ChangeRepository<SugarRepository<ErpProduct>>();
            var productIds = query.Items.Select(m => m.ProductId).ToList();
            var productList = await productRepository.AsQueryable()
                .Where(m => productIds.Contains(m.Id))
                .Includes(m=>m.Type)
                .Includes(m=>m.Unit)
                .Includes(m=>m.Supplier)
                .ToListAsync();
            resultProduct =productList.Adapt<List<ErpProductDto>>();
            foreach (var item in result.Items)
            {
                item.Product = resultProduct.FirstOrDefault(m => m.Id == item.ProductId) ?? new ErpProductDto();
            }
        }
        return result;
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpScrapProductDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<ErpScrapProductDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(ErpScrapProductDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<ErpScrapProduct>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(ErpScrapProductDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<ErpScrapProduct>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
